from pydantic import BaseModel
from typing import List, Dict
from fastapi import BackgroundTasks, FastAPI
from fastapi.middleware.cors import CORSMiddleware
from time import sleep
import json

# from nornir import InitNornir
# from nornir_netmiko import netmiko_send_command
# from nornir_utils.plugins.functions import print_result
# nr = InitNornir(config_file='config.yaml')
# result = nr.run(netmiko_send_command, command_string="show lldp neighbors detail", use_textfsm=True)

app = FastAPI()

origins = [
    "http://localhost.tiangolo.com",
    "https://localhost.tiangolo.com",
    "http://localhost",
    "http://localhost:8080",
    "http://192.168.0.219:3000",
    "http://10.222.2.203:3000",
    'http://192.168.0.210:8080',
]


app.add_middleware(
    CORSMiddleware,
    allow_origins=origins,
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)


class Host(BaseModel):
    name: str = ""
    system_id: str = ""
    mgmt_address: str = ""
    conn_address: str = ""
    # platform_version: str = ""
    system_description: str = ""
    local_ports: List[str] = []
    remote_port: List[str] = []
    capabilities: List[str] = ['R']
    neighbors: List[str] = []


@app.get("/")
async def main():
    return {"message": "Hello World"}


def write_notification(email: str, message=""):
    sleep(20)
    print(f'email: {email}')
    print(f'message: {message}')


@app.post("/send-notification/{email}")
async def send_notification(email: str, background_tasks: BackgroundTasks):
    background_tasks.add_task(write_notification, email, message="some notification")
    return {"message": "Notification sent in the background"}


@app.get("/test01")
async def simple_test_01():
    return {'message': 'test 01'}


@app.get("/test02")
async def simple_test_02():
    nodes = [
        {"id": 0,  "name": "ISP1", "device_type": "cloud", "color": "grey"},
        {"id": 1,  "name": "ISP2", "device_type": "cloud", "color": "grey"},
        {"id": 2,  "name": "Edge1", "device_type": "router", "color": "red"},
        {"id": 3,  "name": "Edge2", "device_type": "router", "color": "red"},
        {"id": 4,  "name": "Switch1", "device_type": "switch"},
        {"id": 5,  "name": "Switch2", "device_type": "switch"},
        {"id": 6,  "name": "ESX1", "device_type": "server"},
        {"id": 7,  "name": "ESX2", "device_type": "server"},
        {"id": 8,  "name": "ESX3", "device_type": "server"},
        {"id": 9,  "name": "ESX4", "device_type": "server"},
        {"id": 10,  "name": "SAN", "device_type": "server"}
    ]

    links = [
        {"source": 0, "target": 2, "color": "green"},
        {"source": 1, "target": 3},
        {"source": 2, "target": 4, "color": "green"},
        {"source": 2, "target": 5},
        {"source": 3, "target": 4},
        {"source": 3, "target": 5},
        {"source": 4, "target": 5},
        {"source": 4, "target": 5},
        {"source": 6, "target": 4, "color": "green"},
        {"source": 6, "target": 5, "color": "red"},
        {"source": 7, "target": 4, "color": "green"},
        {"source": 7, "target": 5, "color": "red"},
        {"source": 8, "target": 4, "color": "green"},
        {"source": 8, "target": 5, "color": "red"},
        {"source": 9, "target": 4, "color": "green"},
        {"source": 9, "target": 5, "color": "red"},
        {"source": 10, "target": 4, "color": "red"},
        {"source": 10, "target": 4, "color": "red"},
        {"source": 10, "target": 5, "color": "red"},
        {"source": 10, "target": 5, "color": "red"}
    ]

    return {'nodes': json.dumps(nodes), 'links': json.dumps(links)}


@app.get("/topo01")
async def simple_topo01():
    nodes = [{'id': 'nxos-84', 'label': 'nxos-84', 'core': 1},
             {'id': 'vEOS-01', 'label': 'vEOS-01', 'core': 1},
             {'id': 'vEOS-2', 'label': 'vEOS-2', 'leaf': 1},
             {'id': 'vEOS-03', 'label': 'vEOS-03', 'leaf': 1},
             {'id': 'vEOS04', 'label': 'vEOS04', 'leaf': 1},
             {'id': 'vMX-20.4R1.12', 'label': 'vMX-20.4R1.12', 'leaf': 1}]
    edges = [{'source': 'vEOS-01', 'target': 'vEOS-2'},
             {'source': 'vEOS-01', 'target': 'vEOS04'},
             {'source': 'vEOS-2', 'target': 'vEOS-03'},
             {'source': 'vEOS-2', 'target': 'vEOS04'},
             {'source': 'vEOS-03', 'target': 'vEOS04'},
             {'source': 'vEOS-03', 'target': 'nxos-84'},
             {'source': 'vEOS-03', 'target': 'vMX-20.4R1.12'}]
    return {'nodes': nodes, 'edges': edges}
